//
//  DlViewController.m
//  UI_豆瓣影院
//
//  Created by dllo on 16/1/4.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "DlViewController.h"
#import "ZcViewController.h"
#import "AAALHXDateHandle.h"
#import "myModel.h"
@interface DlViewController ()<UITextFieldDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UITextField *fie;
@property (nonatomic, retain) UITextField *fie1;
@end

@implementation DlViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
- (void)dealloc {
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView.frame = CGRectMake(0, 0, 375, 667);
    [self.view addSubview:_tableView];
    self.navigationItem.title = @"用户登陆";
    
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(itemAction)];
    self.navigationItem.leftBarButtonItem = item;
   
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(20, 70, 70, 40)];
    //    lable.backgroundColor = [UIColor yellowColor];
    lable.text = @"用户名:";
    [self.view addSubview:lable];
    [lable release];
    
    UILabel *tsLable = [[UILabel alloc] initWithFrame:CGRectMake(120, 110, 150, 30)];
    [self.view addSubview:tsLable];
    tsLable.textColor = [UIColor redColor];
    tsLable.tag = 10000;
    tsLable.font = [UIFont systemFontOfSize:15];
    [tsLable release];
    
    self.fie = [[UITextField alloc] initWithFrame:CGRectMake(120, 70, 150, 40)];
    _fie.placeholder = @"请输入用户名";
    _fie.delegate = self;
    _fie.clearButtonMode = UITextFieldViewModeAlways;
    _fie.clearsOnBeginEditing = YES;
    _fie.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_fie];
    [_fie release];
   
    
    
    UILabel *lable1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 145, 70, 40)];
    //    lable1.backgroundColor = [UIColor yellowColor];
    lable1.text = @"密码:";
    [self.view addSubview:lable1];
    [lable1 release];
    
    UILabel *tsLable1 = [[UILabel alloc] initWithFrame:CGRectMake(120, 185, 150, 30)];
    tsLable1.textColor = [UIColor redColor];
    tsLable1.tag = 10001;
    tsLable1.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:tsLable1];
    [tsLable1 release];
    self.fie1 = [[UITextField alloc] initWithFrame:CGRectMake(120, 145, 150, 40)];
    _fie1.placeholder = @"请输入密码";
    _fie1.keyboardType = UIKeyboardTypePhonePad;
    _fie1.clearsOnBeginEditing = YES;
    _fie1.borderStyle = UITextBorderStyleRoundedRect;
    _fie1.clearButtonMode = UITextFieldViewModeAlways;
    _fie1.delegate = self;
    [self.view addSubview:_fie1];
   
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(20, 220, 130, 50);
    button.backgroundColor = [UIColor greenColor];
    button.layer.cornerRadius = 10;
    [button setTitle:@"登陆" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
   
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake(180, 220, 130, 50);
    button1.backgroundColor = [UIColor orangeColor];
    button1.layer.cornerRadius = 10;
    [button1 setTitle:@"注册" forState:UIControlStateNormal];
    [button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:button1];
    [button1 addTarget:self action:@selector(buttonAction1:) forControlEvents:UIControlEventTouchUpInside];
}


 // 注册按钮
- (void)buttonAction1:(UIButton *)button {
    ZcViewController *vc = [[ZcViewController alloc] init];
   
    // 跳到注册页面
    [self.navigationController pushViewController:vc animated:YES];
    
}

// 设置键盘按位走
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    _fie.tag = 1000;
    _fie1.tag = 1001;
    [textField resignFirstResponder];
    
    UITextField *fie = [self.view viewWithTag:textField.tag + 1];
    
    [fie becomeFirstResponder];
    return YES;
}

// 刚开始编辑，就隐藏lable
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    UILabel *lable1 = [self.view viewWithTag:10001];
     UILabel *lable = [self.view viewWithTag:10000];
    lable1.text = @"";
    lable.text =@"";
    return YES;
}




// 登陆按钮
- (void)buttonAction:(UIButton *)button {
   NSMutableArray *arr = [[AAALHXDateHandle sharedDateBaseHandle]selectTable2];
    myModel *model = [myModel model];
//    model = [arr lastObject];
    if (_fie1.text.length && _fie.text.length) {
        
    
    
    for (model in arr) {
        
    
    if ([model.name isEqualToString: _fie.text] && [model.pwd isEqualToString:_fie1.text] ) {
        
//    }
//       跳回“我”主页
//    if (_fie.text.length && _fie1.text.length) {
        model.type = 0;
        
        [[AAALHXDateHandle sharedDateBaseHandle] updataTable:model];
        [self.myDelegate backTo];
        [self dismissViewControllerAnimated:YES completion:^{
        
        }];
//    } else if (_fie.text.length) {
//        UILabel *lable1 = [self.view viewWithTag:10001];
//        lable1.text = @"请输入密码";
//        
//    } else if (_fie1.text.length) {
//        UILabel *lable = [self.view viewWithTag:10000];
//        lable.text = @"* 请输入用户名";
    } }} else {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名或密码不正确" preferredStyle:(UIAlertControllerStyleAlert)];
        [self presentViewController:alert animated:YES completion:nil];
        [self performSelector:@selector(itemAction2) withObject:alert afterDelay:0.5];
    
    
        
        
    
    
    
    }
   
    
}

// alert方法
- (void)itemAction2 {
    [self dismissViewControllerAnimated:YES completion:nil];
}

// tabbar 返回按钮
- (void)itemAction {
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
